﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace ThreadLearn
{
    internal static class LinkedTokenSourceDemo
    {
        public static void Go()
        {
            CancellationTokenSource cts1 = new CancellationTokenSource();
            CancellationTokenSource cts2 = new CancellationTokenSource();

            cts1.Token.Register(() => Console.WriteLine(string.Format("cts1 IsCancellationRequested {0}", cts1.Token.IsCancellationRequested)));
            cts2.Token.Register(() => Console.WriteLine(string.Format("cts2 IsCancellationRequested {0}", cts2.Token.IsCancellationRequested)));

            CancellationTokenSource linked = CancellationTokenSource.CreateLinkedTokenSource(cts1.Token, cts2.Token);
            linked.Token.Register(() => {
                Thread.Sleep(100);
                Console.WriteLine(string.Format("linked IsCancellationRequested {0}", linked.Token.IsCancellationRequested));
            });

            Console.WriteLine("above");

            cts1.Cancel();

            Console.WriteLine("after");

            Console.ReadLine();

        }
    }
}
